﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace MyChannel.DownloadComponent
{
    public class MyDataContext:DataContext
    {
        public MyDataContext(string connectionString): base(connectionString){ }
        public Table<LocalVideo> Videos;
        public Table<LocalPlaylist> Playlists;
    }


    [Table]
    public class LocalVideo : BaseModel, INotifyPropertyChanging
    {
        private string _id;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public string Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    NotifyPropertyChanging("VideoId");
                    _id = value;
                    OnPropertyChanged("VideoId");
                }
            }
        }


        private string _name;
        [Column]
        public override string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    NotifyPropertyChanging("VideoName");
                    _name = value;
                    OnPropertyChanged("VideoName");
                }
            }
        }

        private string _img;
        [Column]
        public  string Img
        {
            get { return _img; }
            set
            {
                if (_img != value)
                {
                    NotifyPropertyChanging("Img");
                    _img = value;
                    OnPropertyChanged("Img");
                }
            }
        }

    
        private string _state;
        [Column]
        public string State
        {
            get { return _state; }
            set
            {
                if (_state != value)
                {
                    NotifyPropertyChanging("State");
                    _state = value;
                    OnPropertyChanged("State");
                }
            }
        }

        private string _url;
        [Column]
        public string Url
        {
            get { return _url; }
            set
            {
                if (_url != value)
                {
                    NotifyPropertyChanging("Url");
                    _url = value;
                    OnPropertyChanged("Url");
                }
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        // Internal column for the associated ToDoCategory ID value
        [Column]
        internal int _playlistId;

        // Entity reference, to identify the ToDoCategory "storage" table
        private EntityRef<LocalPlaylist> _playlist;

        // Association, to describe the relationship between this key and that "storage" table
        [Association(Storage = "_playlist", ThisKey = "_playlistId", OtherKey = "Id", IsForeignKey = true)]
        public LocalPlaylist Playlist
        {
            get { return _playlist.Entity; }
            set
            {
                NotifyPropertyChanging("Playlist");
                _playlist.Entity = value;

                if (value != null)
                {
                    _playlistId = value.Id;
                }

                NotifyPropertyChanging("Playlist");
            }
        }


        public Dispatcher currentDispatcher;



        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion


    }

    [Table]
    public class LocalPlaylist : BaseModel, INotifyPropertyChanging
    {

        // Define ID: private field, public property, and database column.
        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("Id");
                _id = value;
                OnPropertyChanged("Id");
            }
        }

        // Define category name: private field, public property, and database column.
        private string _name;

        [Column]
        public override string Name
        {
            get { return _name; }
            set
            {
                NotifyPropertyChanging("Name");
                _name = value;
                OnPropertyChanged("Name");
            }
        }


        private string _image;
        [Column]
        public string Image
        {
            get { return _image; }
            set
            {
                NotifyPropertyChanging("Image");
                _image = value;
                OnPropertyChanged("Image");
            }
        }

        private string _key;
        [Column]
        public override string Key
        {
            get { return _key; }
            set
            {
                NotifyPropertyChanging("Key");
                _key = value;
                OnPropertyChanged("Key");
            }
        }

        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        // Define the entity set for the collection side of the relationship.
        private EntitySet<LocalVideo> _videos;

        [Association(Storage = "_videos", OtherKey = "_playlistId", ThisKey = "Id")]
        public EntitySet<LocalVideo> Videos
        {
            get { return this._videos; }
            set { this._videos.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public LocalPlaylist()
        {
            _videos = new EntitySet<LocalVideo>(
                new Action<LocalVideo>(this.attach_ToDo),
                new Action<LocalVideo>(this.detach_ToDo)
                );
        }

        // Called during an add operation
        private void attach_ToDo(LocalVideo video)
        {
            NotifyPropertyChanging("Video");
            video.Playlist = this;
        }

        // Called during a remove operation
        private void detach_ToDo(LocalVideo video)
        {
            NotifyPropertyChanging("Video");
            video.Playlist = null;
        }

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}
